package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.NewTypeModel;
import com.zeng.jiudian.dao.NewTypeDao;
import com.zeng.jiudian.dao.impl.NewTypeDaoImpl;
import com.zeng.jiudian.service.NewTypeService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/8/13 - 14:17
 */
public class NewTypeServiceImpl implements NewTypeService
{
    @Override
    public int add(NewTypeModel newTypeModel)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        int count = newTypeDao.add(newTypeModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NewTypeModel> findAll(NewTypeModel newTypeModel)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        return newTypeDao.findAll(newTypeModel);
    }

    @Override
    public int getCount(NewTypeModel newTypeModel)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        return newTypeDao.getCount(newTypeModel);
    }

    @Override
    public NewTypeModel findById(int id)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        return newTypeDao.findById(id);
    }

    @Override
    public int delete(int id)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        int count = newTypeDao.delete(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public int update(NewTypeModel newTypeModel)
    {
        NewTypeDao newTypeDao = new NewTypeDaoImpl();

        if ("".equals(newTypeModel.getTypeName()))
        {
            newTypeModel.setTypeName(null);
        }

        int count = newTypeDao.update(newTypeModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NewTypeModel> getAll()
    {
        List<NewTypeModel> list = null;
        Jedis jedis = new Jedis();
        jedis.select(0);
        Long length = jedis.llen("newTypeId");

        if (length > 0)
        {
            list = new ArrayList<>();
            List<String> newTypeId = jedis.lrange("newTypeId", 0, length);
            List<String> newTypeName = jedis.lrange("newTypeName", 0, length);
            List<String> newTypeIsShow = jedis.lrange("newTypeIsShow", 0, length);
            List<String> newTypeRecommend = jedis.lrange("newTypeRecommend", 0, length);
            List<String> newTypeCreateTime = jedis.lrange("newTypeCreateTime", 0, length);
            List<String> newTypeUpdateTime = jedis.lrange("newTypeUpdateTime", 0, length);
            for (int i = 0; i < length; i++)
            {
                NewTypeModel model = new NewTypeModel();
                model.setId(Integer.parseInt(newTypeId.get(i)));
                model.setTypeName(newTypeName.get(i));
                model.setIsShow(Integer.parseInt(newTypeIsShow.get(i)));
                model.setRecommend(Integer.parseInt(newTypeRecommend.get(i)));
                model.setCreateTime(newTypeCreateTime.get(i));
                model.setUpdateTime(newTypeUpdateTime.get(i));
                list.add(model);
            }
        }
        else
        {
            NewTypeDao newTypeDao = new NewTypeDaoImpl();
            list = newTypeDao.getAll();
            jedis.del("newTypeId", "newTypeName", "newTypeIsShow", "newTypeRecommend", "newTypeCreateTime", "newTypeUpdateTime");
            for (NewTypeModel model : list)
            {
                jedis.rpush("newTypeId", model.getId().toString());
                jedis.rpush("newTypeName", model.getTypeName());
                jedis.rpush("newTypeIsShow", model.getIsShow().toString());
                jedis.rpush("newTypeRecommend", model.getRecommend().toString());
                jedis.rpush("newTypeCreateTime", model.getCreateTime());
                jedis.rpush("newTypeUpdateTime", model.getUpdateTime());
            }
        }

        return list;
    }
}
